package com.bubblegumproject.ogo.swing.form;

import com.bubblegumproject.ogo.swing.validation.Validator;

import java.util.ArrayList;
import java.util.List;

/**
 * Action generated when a Form has been submitted and validated. The FormValidation indicates whether the Form
 * was successfully validated or not.
 *
 * @author Azubuko Obele (buko.obele@gmail.com)
 */
public class FormValidation extends FormAction {

    protected Object validationContext;

    private List<Validator> failures = new ArrayList<Validator>();

    public FormValidation(Object validationContext) {
        assert(validationContext != null);
        this.validationContext = validationContext;
    }

    public FormValidation(Object validationContext, Validator... validators) {
        assert(validationContext != null);
        this.validationContext = validationContext;
        check(validators);
    }

    public Object getValidationContext() {
        return validationContext;
    }

    public void check(Validator... validators) {
        assert(validators != null);
        for (Validator v : validators) {
            if (!v.isValid()) failures.add(v);
        }
    }

    public boolean failed() {
        return !failures.isEmpty();
    }

    public List<Validator> getFailures() {
        return failures;
    }

    public static FormValidation Validate(Object validationContext, Validator... validators) {
        return new FormValidation(validationContext, validators);
    }
}
